package com.sean.common.page;

import org.apache.ibatis.session.RowBounds;
/**
 * 分页查询约束.
 * @author XinWang
 *
 */
public class PageRowBounds extends RowBounds {
    
    private int pageSize = 0;// 每页记录数
	
    private int pageNum = 1;// 当前页数
    
    private int pageCount = 1;// 总页数
    
    private int rowCount = 0;
    
    private boolean next = true;// 是否能下一页
	
    private boolean previous = true;// 是否能上一页
    
   public PageRowBounds(int currentPage) {
   	this.pageNum = currentPage;
   	if(this.pageNum == 0) {
   	 this.pageNum = 1;
   	}
    }
    
    public PageRowBounds(Pagination pagination) {
   	this.pageNum = pagination.getPageNum();
   	if(this.pageNum == 0) {
   	    this.pageNum = 1;
      	}
   	this.pageSize = pagination.getPageSize();
    }

    public void setCount(int rowCount) {
	this.rowCount = rowCount;
	this.pageCount = (int)Math.ceil((float)rowCount/(float)pageSize);
	if(this.pageCount == 0) {
	    this.pageCount = 1;
	}
    }

    public int getOffsetForPagination() {
	if(pageNum == 1) {
	    this.previous = false;
	    return 0;
	}
	return (pageNum - 1) * pageSize;
    }

    public int getLimitForPagination() {
	int Offset = this.getOffsetForPagination();
	if(rowCount <= (Offset + pageSize)) {
	    this.next = false;
	    return rowCount-Offset;
	}
	return pageSize;//当前页结束记录位置
    }

    public boolean isNext() {
        return next;
    }

    public boolean isPrevious() {
        return previous;
    }
    

    public int getPageSize() {
        return pageSize;
    }
    

    public int getPageNum() {
		return pageNum;
	}

	public int getPageCount() {
        return pageCount;
    }

    public int getRowCount() {
        return rowCount;
    }

    public void setPageSize(int pageSize) {
	this.pageSize = pageSize;	
    }
}
